/*
 *  TO BE USED BY VERN 
 * Author: Tom Ajayebi
 * Class: COIN 71
 * Filename: fetchRoute.js
 *
 * Date Created: 3/04/2009
 * functionality: Send MYSQL query to server side PHP to retrive rows from mysql.
 */


var URL = "mysqlQuery.php";
//var URL = "http://ctislab.fhda.edu/ajax/mysqlQuery.php";
var myQUERY = null;
var xmlDoc  =  null;

// Result of SELECT query ,give the result back to the calling routine to do whatever they like.
function selectQueryResult(){
    xmlDoc = this.req.responseText;
 
    // convert returned XML into JSON
    var myJsonObject=xml2json.parser(xmlDoc,'','normal');

    // DEBUG: alert returned result in JSON
    alert(myJsonObject);
    return false;
}

// Construct a SQL query for retriving fields inside mysql.
function viewGroupRides(){
myQUERY = "q=SELECT id,title,city,start_point,day,date,time,leader,leader_phone,description,category,hilliness,mileage FROM mazaika1.rides";
 
 //call content loader with the query constructed.
 contentLoader = new net.ContentLoader(URL, selectQueryResult, errorFunction, "POST", myQUERY);

}

// Result of INSERT query, just give a message back to user if the insert succeed.
function insertQueryResult(){
    xmlDoc = this.req.responseText;

    //DEBUG: display returned xml for debug which contains INSERT into DB
    alert(xmlDoc);

}

// INSERT Operation: Grab user inputed data and INSERT it into mysql DB table
function sendQuery(){

    // get field values from HTML page to construct INSERT query
    var title_value = document.getElementById("title").firstChild.value  ;
    var city_value = document.getElementById("city").firstChild.value ;
    var start_point_value = document.getElementById("start_point").firstChild.value ;
    var day_value = document.getElementById("day").firstChild.value ;
    var date_value = document.getElementById("date").firstChild.value ;
    var time_value = document.getElementById("time").firstChild.value ;
    var leader_value = document.getElementById("leader").firstChild.value ;
    var leader_phone_value = document.getElementById("leader_phone").firstChild.value ;
    var description_value = document.getElementById("description").firstChild.value ;
    var category_value = document.getElementById("category").firstChild.value ;   //  # ADDed by Tom
    var hilliness_value = document.getElementById("hilliness").firstChild.value ;       //  Added by Tom
    var mileage_value = document.getElementById("mileage").firstChild.value ;   //  # Added By Tom 
    
    
    // Check to see if all the fields are inputed by user.       
        if ((title_value == "")  || (city_value == "" )       || (start_point_value == "") ||
        (day_value == "")    || (date_value == "")        || (time_value == "")        ||
        (leader_value == "") || (leader_phone_value == "") || (description_value == "") || 
        (category_value == "")) ||  (hilliness_value == "") || (mileage_value == "") {
         alert("All need to be input, no NULL value is permitted");
    }
    else {
        myQUERY = "q=INSERT INTO mazaika1.rides (title,city,start_point,day,date,time,leader,leader_phone,description,category,hilliness,mileage) VALUES ('"+title_value+"','"+city_value+"','"+start_point_value+"','"+day_value+"','"+date_value+"','"+time_value+"','"+leader_value+"','"+leader_phone_value+"','"+description_value+"','"+category_value+"','"+hilliness+"','"+mileage+"')";
        contentLoader = new net.ContentLoader(URL, insertQueryResult, errorFunction, "POST", myQUERY);
        alert("success.");
    }
    return false;
}
